﻿using FlaUI.UIA3;
using System.Diagnostics;
using System;
using System.IO;
using FlaUI.Core;
using FlaUI.Core.AutomationElements;
using System.Linq;
using FlaUI.Core.Definitions;


public class WXWork5FunctionalAutomation
{
    private Window _window;
    private WXWork5CoordinateMapper _coordMapper;

    public bool Initialize()
    {
        try
        {
            System.Threading.Thread.Sleep(8000); // 5.0版本启动较慢

            var processes = Process.GetProcessesByName("WXWork");
            if (processes.Length == 0)
            {
                // 企业微信 5.0 常见安装路径
                string[] paths = {
                    @"D:\Program Files (x86)\WXWork\WXWork.exe",
                    Environment.ExpandEnvironmentVariables(@"%USERPROFILE%\AppData\Local\Programs\WXWork\WXWork.exe")
                };

                foreach (var path in paths)
                {
                    if (File.Exists(path))
                    {
                        var app = Application.Launch(path);
                        System.Threading.Thread.Sleep(8000); // 5.0版本启动较慢
                        break;
                    }
                }
            }

            if (processes.Length > 0)
            {
                var app = Application.Attach(processes[0]);
                using (var automation = new UIA3Automation())
                {
                    _window = app.GetMainWindow(automation);
                    _coordMapper = new WXWork5CoordinateMapper(_window);
                    return _window != null;
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"初始化失败: {ex.Message}");
        }
        return false;
    }

    public void OpenContacts()
    {
        Console.WriteLine("打开通讯录...");
        _coordMapper.ClickRelative(0.03, 0.20); // 通讯录按钮
        System.Threading.Thread.Sleep(2000);
    }

    public void SearchContact(string contactName)
    {
        Console.WriteLine($"搜索联系人: {contactName}");

        // 点击搜索框
        _coordMapper.ClickRelative(0.25, 0.06);
        System.Threading.Thread.Sleep(1000);

        // 输入搜索内容
        System.Windows.Forms.SendKeys.SendWait(contactName);
        System.Threading.Thread.Sleep(2000);

        // 按回车搜索
        System.Windows.Forms.SendKeys.SendWait("{ENTER}");
        System.Threading.Thread.Sleep(2000);
    }

    public void SendMessageToCurrentContact(string message)
    {
        Console.WriteLine($"发送消息: {message}");

        // 点击消息输入框
        _coordMapper.ClickRelative(0.35, 0.85);
        System.Threading.Thread.Sleep(500);

        // 输入消息
        System.Windows.Forms.SendKeys.SendWait(message);
        System.Threading.Thread.Sleep(1000);

        // 点击发送按钮
        _coordMapper.ClickRelative(0.92, 0.85);
        System.Threading.Thread.Sleep(1000);
    }

    public void SwitchToWorkbench()
    {
        Console.WriteLine("切换到工作台...");
        _coordMapper.ClickRelative(0.03, 0.28); // 工作台按钮
        System.Threading.Thread.Sleep(2000);
    }

    public void TestAllMainFunctions()
    {
        Console.WriteLine("=== 测试企业微信 5.0 主要功能 ===");

        // 测试消息功能
        Console.WriteLine("\n1. 测试消息功能");
        _coordMapper.ClickRelative(0.03, 0.12);
        System.Threading.Thread.Sleep(2000);

        // 测试通讯录
        Console.WriteLine("\n2. 测试通讯录功能");
        OpenContacts();
        SearchContact("测试");

        // 测试工作台
        Console.WriteLine("\n3. 测试工作台功能");
        SwitchToWorkbench();

        // 返回消息界面
        Console.WriteLine("\n4. 返回消息界面");
        _coordMapper.ClickRelative(0.03, 0.12);
        System.Threading.Thread.Sleep(2000);

        Console.WriteLine("功能测试完成");
    }
}
